<?php
namespace App\Services;
use App\Models\ActivityModel;
use App\Models\AreaModel;
use App\Models\MemberActivityModel;
use App\Models\MemberModel;
use App\Models\BabyModel;
use Illuminate\Support\Facades\DB;

class ActivityService{
    /**
     * 查看活动报名列表
     * @param int $activityId
     * @return object $signList
     */
    public function getSignActivityList($activityId){
        $sql = "SELECT ma.id,ma.activity_id,ma.member_id,o.ordernum,o.pay_status,o.product_type FROM bgz_member_activity ma LEFT JOIN bgz_order o ON ma.member_id = o.member_id WHERE ma.activity_id = {$activityId} AND o.product_type = 4 AND o.pay_status = 40 ORDER BY ma.id DESC";
        // $signList = MemberActivityModel::where('activity_id',$activityId)->get();
        $signList = DB::select($sql);

        if(empty($signList)){
            return false;
        }else{
            foreach($signList as $stm=>$sts){
                $memberInfo = MemberModel::where('id',$sts->member_id)->select('id','nick','telephone')
                    ->first();

                if($memberInfo->id > 116 && $memberInfo->nick == base64_encode(base64_decode($memberInfo->nick))){
                    $memberInfo->nick = base64_decode($memberInfo->nick);
                }

                $signList[$stm]->nick = $memberInfo->nick;
                if($memberInfo->telephone != ''){
                    $signList[$stm]->telephone = $memberInfo->telephone;
                }else{
                    $signList[$stm]->telephone = '-';
                }

                $babyList = BabyModel::where('member_id',$sts->member_id)->select('baby_nick','baby_age')->get();
                $signList[$stm]->babyList = $babyList;

                unset($memberInfo);
            }

            return $signList;
        }
    }

    /**
     * 获取会员报名活动列表
     * @param int $member_id
     * @return object $malist
     */
    public function getMemberActivityList($member_id){
        $malist = MemberActivityModel::where('member_id',$member_id)->get();
        if(empty($malist)){
            return false;
        }

        foreach($malist as $mam=>$mat){
            $malist[$mam]->activityInfo = $this->getActivityInfoById($mat->activity_id);
        }

        return $malist;
    }

    /**
     * 添加活动报名信息
     * @param array $data
     * @return bool $res
     */
    public function insertMemberActivity($data){
        $res = MemberActivityModel::insert($data);
        return $res;
    }


	/**
	 * 获取已经报名的活动id
	 * @param int $member_id
	 * @param int $activity_id
	 * @return object $info
	 */
	public function getMemberActivityInfo($member_id,$activity_id){
		$info = MemberActivityModel::where('member_id',$member_id)->where('activity_id',$activity_id)->first();
		return $info;
	}

	/**
	 * 获取正在进行中活动总数
	 * @param int $city
	 * @return int $num
	 */
	public function getOngoingActivityCount($city){
		$num = ActivityModel::where('status',1)->where('city',$city)->count();
		return $num;
	}

	/**
	 * 获取正在进行中活动列表
	 * @param int $city
	 * @return object $activitylist
	 */
	public function getOngoingActivityList(){
		$activitylist = ActivityModel::where('status',1)->select('id','name','desc','thumb','start_date','end_date','address','province','city','county','price','enrolment')->get()->toArray();

		foreach($activitylist as $atm=>&$ast){
			$province = $this->getPcInfo($ast['province']);
			$city = $this->getPcInfo($ast['city']);
			$county = $this->getPcInfo($ast['county']);

			$ast['n_address'] = $province->name.$city->name.$county->name.$ast['address'];
			unset($province,$city,$county);
		}

		return $activitylist;	
	}


	/**
	 * 获取活动列表
	 * @param int $pagesize
	 * @param string $keywords
	 * @return object $activitylist
	 */
	public function getActivityList($pagesize,$keywords=''){
		$activitylist = ActivityModel::orderBy('id','desc')
						->where('isshow',1)
						->where(function($query) use ($keywords){
							if($keywords){
								$query->where('name', 'like', '%'.$keywords.'%');
							}
						})
						
						->select('id','name','desc','thumb','start_date','end_date','address','price','enrolment','status')
						->paginate($pagesize);

		return $activitylist;
	}

	/**
	 * 获取活动详细信息
	 * @param string $name
	 * @return object $info
	 */
	public function getActivityInfoByName($name){
		$info = ActivityModel::where('name',$name)->select('id')->first();
		return $info;
	}

	/**
	 * 获取活动详细信息
	 * @param int $id
	 * @return object $info
	 */
	public function getActivityInfoById($id){
		$info = ActivityModel::where('id',$id)->first();
		return $info;
	}

	/**
	 * 添加活动
	 * @param array $data
	 * @return bool $res
	 */
	public function insertActivity($data){
		$res = ActivityModel::insert($data);
		return $res;
	}

	/**
	 * 更新活动
	 * @param int $id
	 * @param array $data
	 * @param bool $res
	 */
	public function modiActivity($id,$data){
		$res = ActivityModel::where('id',$id)->update($data);
		return $res;
	}

	/**
	 * 获取省市县信息
	 * @param $id
	 * @return object $info
	 */
	private function getPcInfo($id){
		$info = AreaModel::where('id',$id)->first();
		return $info;
	}
}